﻿
using System;
using System.Data;
using System.Collections.Generic;
namespace ZHDJ.MobileInterface
{
    public partial class WXBinding : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {  
            // 业务逻辑返回的响应对象  
            WXBindingOutParam result = new WXBindingOutParam();
            WXBindingInParam param = new WXBindingInParam();
            string logMsg = "";
            bool isFlag = true;

            try
            {
                param = inParam as WXBindingInParam;
                if(string.IsNullOrEmpty(param.openid))
                {
                    result.code = "0";
                    result.desc = "微信号不能为空";

                    isFlag = false;
                }

                if (string.IsNullOrEmpty(param.login_name) || string.IsNullOrEmpty(param.pwd))
                {
                    result.code = "0";
                    result.desc = "用户名/身份证/手机号码，密码不能为空";

                    isFlag = false;
                }

                if (isFlag)
                {
                    if (CommonUtils.IsDisableUsers(param.login_name))
                    {
                        result.code = "0";
                        result.desc = "用户状态有问题，请联系管理员";

                        isFlag = false;
                    }
                }

                // 登陆是否成功 
                string loginID = "";
                if(isFlag)
                { 
                    DataSet dsMember;
                    if (param.type == "2")
                    {
                        dsMember = InterfaceHelper.GetMembersByFilter(" MobileNo='" + param.login_name + "' ");
                        if (dsMember == null || dsMember.Tables[0].Rows.Count == 0)
                        {
                            result.code = "0";
                            result.desc = "手机号码不存在";

                            isFlag = false;
                        }
                        else if (dsMember.Tables[0].Rows.Count > 1)
                        {
                            result.code = "0";
                            result.desc = "手机号码不唯一,请改用登陆帐号或身份证方式登陆";

                            isFlag = false;
                        }
                        else
                        {
                            loginID = dsMember.Tables[0].Rows[0]["LoginID"].ToString();
                        }
                    }
                    else if (param.type == "3")
                    {
                        dsMember = InterfaceHelper.GetMembersByFilter(" IdentityCard='" + param.login_name + "' ");
                        if (dsMember == null || dsMember.Tables[0].Rows.Count == 0)
                        {
                            result.code = "0";
                            result.desc = "身份证号码不存在";

                            isFlag = false;
                        }
                        else if (dsMember.Tables[0].Rows.Count > 1)
                        {
                            result.code = "0";
                            result.desc = "身份证号码不唯一,请改用登陆帐号或手机号码方式登陆";

                            isFlag = false;
                        }
                        else
                        {
                            loginID = dsMember.Tables[0].Rows[0]["LoginID"].ToString();
                        }
                    }
                    else
                    {
                        loginID = InterfaceHelper.GetLoginID(param.login_name);
                        if (string.IsNullOrEmpty(loginID))
                        {
                            result.code = "0";
                            result.desc = "登录账号不存在";

                            isFlag = false;
                        }
                    }
                }
                 
                if (isFlag)
                {
                    param.pwd = CommonUtils.GetMD5(param.pwd.ToLower());
                    isFlag = InterfaceHelper.PasswordIsCorrect(loginID, param.pwd);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "用户名，密码输入不正确";
                    }
                }

                if (isFlag)
                {
                    if (!InterfaceHelper.LoginIDEnable(loginID))
                    { 
                        result.code = "0";
                        result.desc = "您的账号还没启用，请联系系统管理员";

                        isFlag = false;
                    }
                }
                 
                // 微信号是否已经绑定 
                if (isFlag)
                {
                    if (InterfaceHelper.WXIsHaveBinding(loginID, param.openid))
                    {
                        result.code = "0";
                        result.desc = "该微信号或者该用户已经进行过绑定";

                        isFlag = false;
                    }
                }

                // 绑定微信号 
                if (isFlag)
                {
                    if (!InterfaceHelper.BindingWX(loginID, param.openid))
                    {
                        result.code = "0";
                        result.desc = "绑定失败，请重新尝试";

                        isFlag = false;
                    }
                    else if (param.addScore != "0")
                    {
                        //增加微信绑定积分
                        InterfaceHelper.ScoreManagePartyWXBDAdd(loginID);
                    }
                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-UserWXBinding", SerializeHelper.JsonSerializer<WXBindingInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<WXBindingInParam, WXBindingOutParam>();
            else
                SafeProcessRequest<WXBindingInParam, WXBindingOutParam>();
        }
    }

    public class WXBindingInParam
    {
        /// <summary>
        ///	微信号
        /// </summary>
        public string openid
        { get; set; }

        /// <summary>
        ///	用户名
        /// </summary>
        public string login_name
        { get; set; }

        /// <summary>
        ///	密码
        /// </summary>
        public string pwd
        { get; set; }

        /// <summary>
        ///	登陆类型 1 用户名;2 手机号码;3身份证;(为空时,默认为使用用户名)
        /// </summary>
        public string type
        { get; set; }

        /// <summary>
        ///	是否增加积分（1 增加，0不增加），默认为1
        /// </summary>
        public string addScore
        { get; set; }
    }

    public class WXBindingOutParam
    {
        public WXBindingOutParam()
        {
            code = "1";
            desc = "操作成功"; 
        }

        public string code
        { get; set; }

        public string desc
        { get; set; } 
    } 
}